package com.zhl.media.login.mvp.presenter;

import android.app.Application;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;
import com.zhl.media.commonsdk.base.BaseRespose;
import com.zhl.media.commonsdk.data.UserUtil;
import com.zhl.media.commonsdk.entity.Token;
import com.zhl.media.commonsdk.entity.User;
import com.zhl.media.login.mvp.contract.LoginContract;

import org.json.JSONObject;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;
import retrofit2.HttpException;


@ActivityScope
public class LoginPresenter extends BasePresenter<LoginContract.Model, LoginContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public LoginPresenter(LoginContract.Model model, LoginContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void token(String user,String psw){
        mModel.getToken(user,psw)
            .subscribeOn(Schedulers.io())
            .retryWhen(new RetryWithDelay(0, 0))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
            .doOnSubscribe(disposable -> {
                mRootView.showLoading();
            }).subscribeOn(AndroidSchedulers.mainThread())
            .observeOn(AndroidSchedulers.mainThread())
            .doFinally(() -> {
                mRootView.hideLoading();
            })
            .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
            .subscribe(new ErrorHandleSubscriber<Token>(mErrorHandler) {
                @Override
                public void onNext(Token token) {
                    UserUtil.saveOldAcount(user);
                    UserUtil.saveOldPass(psw);
                    mRootView.token(token);
                }
            });
    }

    public void userInfo(){
        mModel.getUserInfo()
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<Object>(mErrorHandler) {
                    @Override
                    public void onNext(Object user) {
                        Gson gson = new Gson();
                        String jsonStr = gson.toJson(user);
                        JsonObject jsonObject = gson.fromJson(jsonStr, JsonObject.class);
                        User principal = gson.fromJson(jsonObject.getAsJsonObject("principal"), User.class);
                        mRootView.userInfo(principal);
                    }
                });
    }

}
